package com.yichuang.wuziqi.tool;

import android.graphics.Point;

import java.util.List;

/**
 * Created by xcm on 17-11-26.
 */

public class GameTools {
    private static final int MAX_COUNT_IN_LINE = 5;

    public static boolean checkFiveInLine(List<Point> points) {

        for (Point p : points) {
            int x = p.x;
            int y = p.y;

            boolean win = checkHorizontal(x, y, points);
            if (win) return true;
            win = checkVertical(x, y, points);
            if (win) return true;
            win = checkLeftUpRightDown(x, y, points);
            if (win) return true;
            win = checkLeftDownRightUp(x, y, points);
            if (win) return true;
        }

        return false;
    }

    /**
     * 判断x,y位置横向方向上是否有五个相邻的位置
     *
     * @param x
     * @param y
     * @param points
     * @return
     */
    private static boolean checkHorizontal(int x, int y, List<Point> points) {
        int count = 1;
        //向左找
        for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
            if (points.contains(new Point(x - i, y))) {
                count++;
            } else {
                break;
            }
        }
        //向右找
        for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
            if (points.contains(new Point(x + i, y))) {
                count++;
            } else {
                break;
            }
        }
        return count == MAX_COUNT_IN_LINE;
    }

    /**
     * 判断x,y位置纵向方向上是否有五个相邻的位置
     *
     * @param x
     * @param y
     * @param points
     * @return
     */
    private static boolean checkVertical(int x, int y, List<Point> points) {
        int count = 1;
        //向上找
        for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
            if (points.contains(new Point(x, y - i))) {
                count++;
            } else {
                break;
            }
        }
        //向下找
        for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
            if (points.contains(new Point(x, y + i))) {
                count++;
            } else {
                break;
            }
        }
        return count == MAX_COUNT_IN_LINE;
    }

    /**
     * 判断x,y位置左斜方向上是否有五个相邻的位置
     *
     * @param x
     * @param y
     * @param points
     * @return
     */
    private static boolean checkLeftUpRightDown(int x, int y, List<Point> points) {
        int count = 1;
        //向左上找
        for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
            if (points.contains(new Point(x - i, y - i))) {
                count++;
            } else {
                break;
            }
        }
        //向右下找
        for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
            if (points.contains(new Point(x + i, y + i))) {
                count++;
            } else {
                break;
            }
        }
        return count == MAX_COUNT_IN_LINE;
    }

    /**
     * 判断x,y位置左斜方向上是否有五个相邻的位置
     *
     * @param x
     * @param y
     * @param points
     * @return
     */
    private static boolean checkLeftDownRightUp(int x, int y, List<Point> points) {
        int count = 1;
        //向左下找
        for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
            if (points.contains(new Point(x - i, y + i))) {
                count++;
            } else {
                break;
            }
        }
        //向右上找
        for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
            if (points.contains(new Point(x + i, y - i))) {
                count++;
            } else {
                break;
            }
        }
        return count == MAX_COUNT_IN_LINE;
    }
}
